package com.imooc.controller;

import com.imooc.common.dto.OrderDTO;
import com.imooc.common.enums.ResultEnum;
import com.imooc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/seller/order")
public class SellOrderController {

    @Autowired
    OrderService orderService;

    /**
     * 订单列表
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<OrderDTO> orderDTOList = orderService.findAll(pageRequest);
        Map<String, Object> param = new HashMap();
        param.put("orderDTOPage", orderDTOList);
        param.put("currentPage", page);
        param.put("currentSize", size);
        return new ModelAndView("order/list", param);
    }

    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/cancel")
    public ModelAndView cancel(String orderId) {
        OrderDTO orderDTO = null;
        Map<String, Object> params = new HashMap<>();
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        } catch (Exception e) {
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/list");
            return new ModelAndView("common/error", params);
        }

        params.put("msg", "上架成功");
        params.put("url", "/sell/seller/product/list");
        return new ModelAndView("common/success", params);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = null;
        Map<String, Object> params = new HashMap<>();
        try {
            orderDTO = orderService.findOne(orderId);
        } catch (Exception e) {
            log.error("查询订单详情页异常");
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/list");
            return new ModelAndView("common/error", params);
        }

        params.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail", params);
    }

    /**
     *  完结订单
     * @param orderId
     * @return
     */
    @RequestMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = null;
        Map<String, Object> params = new HashMap<>();
        try {
            orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        } catch (Exception e) {
            log.error("完结订单异常");
            params.put("msg", e.getMessage());
            params.put("url", "/sell/seller/list");
            return new ModelAndView("common/error", params);
        }

        params.put("msg", ResultEnum.CANCEL_ORDER_SUCCESS.getMessage());
        params.put("url", "/sell/seller/list");
        return new ModelAndView("common/success", params);
    }
}
